<?php

class FlagUtils {

	function createFlags (&$bean, $event, $arguments)
	{
		global $db;
//$GLOBALS['log']->fatal(print_r($bean, true));
		if(empty($bean->fetched_row) && isset($bean->related_editor_id) && $bean->type == 'InternalQualityReview'){
			require_once('modules/edanz_Deliverables/edanz_Deliverables.php');
                	$deliverable = new edanz_Deliverables();
                	$deliverable->retrieve($bean->deliverable_id);
                	if(!empty($deliverable) && !empty($deliverable->service)){
                                $bean->deliverable_service = $deliverable->service;
                        }

			require_once('modules/edanz_Editor/edanz_Editor.php');
			$editor = new edanz_Editor();
			$editor->retrieve($bean->related_editor_id);
			if(!empty($editor->id)){
				if($editor->isNewEditor() && $bean->deliverable_service == 'FirstEdit'){
					require_once('modules/edanz_Flags/edanz_Flags.php');
					$flag = new edanz_Flags();
					$flag->name = 'NewEditor';
//					$flag->deliverable_id = $bean->deliverable_id;
					$flag->save();
					
					$flags_id = $flag->id ;
					$deliverable_id = $bean->deliverable_id;
					$query_insert="INSERT INTO edanz_deliverables_flags VALUES (UUID(), NOW(), 0, '$flags_id', '$deliverable_id')";  
					$db->query($query_insert); 
				}
				if($editor->isOnProbation() && ($bean->deliverable_service == 'FirstEdit' || $bean->deliverable_service == 'Clarification')){
					require_once('modules/edanz_Flags/edanz_Flags.php');
					$flag = new edanz_Flags();
					$flag->name = 'ProbationEditor';
//					$flag->deliverable_id = $bean->deliverable_id;
					$flag->save();
					
					$flags_id = $flag->id ;
					$deliverable_id = $bean->deliverable_id;
					$query_insert="INSERT INTO edanz_deliverables_flags VALUES (UUID(), NOW(), 0, '$flags_id', '$deliverable_id')";  
					$db->query($query_insert);  
				}
			}
		}
	}
}
?>
